package com.card.server;

import java.util.List;

import javax.jdo.PersistenceManager;

import com.card.client.entry.CreditCard;
import com.card.client.service.CreditCardService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("unchecked")
public class CreditCardImpl extends RemoteServiceServlet implements	CreditCardService {

	private static final long serialVersionUID = -4096019293947873205L;

	@Override
	public void addCreditCard(CreditCard card) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		if(card!=null){
			pm.makePersistent(card);
		}
		pm.close();
	}

	@Override
	public void delCreditCard(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		if(id!=null){
			String query = "select from " + CreditCard.class.getName() + " where id == " + id;
			List<CreditCard> result = (List<CreditCard>)pm.newQuery(query).execute(); 
			CreditCard cc = result.get(0);
			pm.deletePersistent(cc);
		}
		pm.close();
	}
	
	@Override
	public CreditCard[] getAllCard() {
		CreditCard[] result = null;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<CreditCard> list = (List<CreditCard>)pm.newQuery(CreditCard.class).execute();
		if(list!=null){
			result = new CreditCard[list.size()];
			for(int i=0;i<result.length;i++){
				result[i] = list.get(i);
			}
		} else {
			result = new CreditCard[0];
		}
		pm.close();
		return result;
	}

	@Override
	public void updateCreditCard(CreditCard card) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		if(card!=null){
			pm.makePersistent(card);
		}
		pm.close();
	}

}
